package org.example.leetCode;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class intersect {
    public static void main(String[] args) {
        int[] nums1=new int[]{1,2,2,1};
        int[] nums2= new int[]{2,2};
        int[] ints = intersect1(nums1, nums2);
        System.out.println(Arrays.toString(ints));

    }

    public static int[] intersect1(int[] nums1, int[] nums2) {
        List<Integer> list = new LinkedList();
        int[] min = nums1.length > nums2.length ? nums2 : nums1;
        int[] max = nums1.length < nums2.length ? nums2 : nums1;
        int maxLen = max.length;
        for (int i = 0; i < min.length; i++) {
            for (int j = 0; j < maxLen; j++) {
                if (min[i]==max[j]){
                    list.add(min[i]);
                    if (j!=maxLen-1){
                        int n = max[j];
                        max[j]=max[maxLen-1];
                        max[--maxLen]=n;
                    }
                    break;
                }
            }
        }
        int index = 0;
        int[] res = new int[list.size()];
        for (int k = 0; k < list.size(); k++) {
            res[index++] = list.get(k);
        }
        return res;
    }
}
